<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单并发处理解决方案</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .mermaid {
            background: white;
            border-radius: 12px;
            padding: 2rem;
        }
        .code-block {
            background: #1e293b;
            color: #e2e8f0;
            border-radius: 12px;
            overflow-x: auto;
        }
        .number-circle {
            width: 3rem;
            height: 3rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 700;
            font-size: 1.25rem;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    <i class="fas fa-sync-alt mr-4"></i>订单并发处理解决方案
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    当用户在订单取消时刚好付款，如何确保系统的数据一致性和事务完整性？
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                        <i class="fas fa-shield-alt mr-2"></i>事务管理
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                        <i class="fas fa-sync mr-2"></i>最终一致性
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                        <i class="fas fa-code mr-2"></i>实战方案
                    </span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="max-w-4xl mx-auto mb-16">
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <p class="text-lg leading-relaxed text-gray-700 drop-cap">
                    在实际项目中处理用户在订单取消时刚好付款的情况需要设计一个健壮的系统来确保数据一致性和事务完整性。这是一个典型的分布式系统并发问题，需要综合运用多种技术手段来解决。
                </p>
            </div>
        </div>

        <!-- Solutions Grid -->
        <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-8 mb-16">
            <!-- Solution 1 -->
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-start mb-4">
                    <div class="number-circle mr-4">1</div>
                    <h3 class="text-2xl font-bold text-gray-800">事务管理</h3>
                </div>
                <p class="text-gray-600 leading-relaxed mb-4">
                    确保订单的创建、付款和取消操作都在同一个事务中执行。使用分布式事务管理器（如 XA 事务）或本地事务来保证所有操作要么全都成功，要么全都失败。
                </p>
                <div class="flex items-center text-purple-600">
                    <i class="fas fa-database mr-2"></i>
                    <span class="text-sm font-medium">ACID 保证</span>
                </div>
            </div>

            <!-- Solution 2 -->
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-start mb-4">
                    <div class="number-circle mr-4">2</div>
                    <h3 class="text-2xl font-bold text-gray-800">异步处理与补偿</h3>
                </div>
                <p class="text-gray-600 leading-relaxed mb-4">
                    通过消息队列等机制将支付和取消操作解耦，确保操作的可靠性。实现补偿机制，以应对异步操作中的失败情况。
                </p>
                <div class="flex items-center text-purple-600">
                    <i class="fas fa-exchange-alt mr-2"></i>
                    <span class="text-sm font-medium">Saga 模式</span>
                </div>
            </div>

            <!-- Solution 3 -->
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-start mb-4">
                    <div class="number-circle mr-4">3</div>
                    <h3 class="text-2xl font-bold text-gray-800">幂等性设计</h3>
                </div>
                <p class="text-gray-600 leading-relaxed mb-4">
                    确保操作的幂等性，即无论操作执行多少次，结果都是一致的。通过唯一的订单号和支付流水号进行去重。
                </p>
                <div class="flex items-center text-purple-600">
                    <i class="fas fa-fingerprint mr-2"></i>
                    <span class="text-sm font-medium">唯一标识</span>
                </div>
            </div>

            <!-- Solution 4 -->
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-start mb-4">
                    <div class="number-circle mr-4">4</div>
                    <h3 class="text-2xl font-bold text-gray-800">订单状态管理</h3>
                </div>
                <p class="text-gray-600 leading-relaxed mb-4">
                    使用订单状态来控制操作的流程。支付后订单状态应更新为"已支付"，取消操作应根据订单状态决定是否允许。
                </p>
                <div class="flex items-center text-purple-600">
                    <i class="fas fa-traffic-light mr-2"></i>
                    <span class="text-sm font-medium">状态机</span>
                </div>
            </div>

            <!-- Solution 5 -->
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-start mb-4">
                    <div class="number-circle mr-4">5</div>
                    <h3 class="text-2xl font-bold text-gray-800">最终一致性</h3>
                </div>
                <p class="text-gray-600 leading-relaxed mb-4">
                    确保系统状态最终一致。订单取消时检查是否已付款，如已完成则进行退款处理或标记为已支付并通知用户。
                </p>
                <div class="flex items-center text-purple-600">
                    <i class="fas fa-check-double mr-2"></i>
                    <span class="text-sm font-medium">BASE 理论</span>
                </div>
            </div>

            <!-- Solution 6 -->
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-start mb-4">
                    <div class="number-circle mr-4">6</div>
                    <h3 class="text-2xl font-bold text-gray-800">监控与告警</h3>
                </div>
                <p class="text-gray-600 leading-relaxed mb-4">
                    建立监控系统以检测和告警可能出现的异常情况。监控系统可以帮助及时发现和处理问题，避免用户体验受影响。
                </p>
                <div class="flex items-center text-purple-600">
                    <i class="fas fa-chart-line mr-2"></i>
                    <span class="text-sm font-medium">实时监控</span>
                </div>
            </div>
        </div>

        <!-- System Architecture -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-16">
            <h2 class="text-3xl font-bold text-gray-800 mb-8 text-center">
                <i class="fas fa-project-diagram mr-3 text-gradient"></i>系统架构设计
            </h2>
            <div class="mermaid">
                graph TB
                    A[用户请求] --> B{订单服务}
                    B --> C[创建订单]
                    B --> D[取消订单]
                    C --> E[Kafka消息队列]
                    D --> E
                    E --> F[支付服务]
                    E --> G[退款服务]
                    F --> H[更新订单状态]
                    G --> H
                    H --> I[数据库]
                    I --> J[监控系统]
                    J --> K[告警通知]
                    
                    style A fill:#f9f,stroke:#333,stroke-width:2px
                    style B fill:#bbf,stroke:#333,stroke-width:2px
                    style E fill:#fbf,stroke:#333,stroke-width:2px
                    style I fill:#bfb,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Code Example -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-16">
            <h2 class="text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-code mr-3 text-gradient"></i>实际应用示例
            </h2>
            <p class="text-gray-600 mb-6">
                假设使用 Spring Boot 和 Kafka 实现异步处理，以下是订单取消服务的核心代码：
            </p>
            <div class="code-block p-6">
                <pre><code class="text-sm">// 订单取消服务示例
@Transactional
public void cancelOrder(String orderId) {
    Order order = orderRepository.findById(orderId)
        .orElseThrow(() -> new OrderNotFoundException(orderId));
    
    if (order.getStatus() == OrderStatus.PAID) {
        // 发送退款请求
        paymentService.refund(order.getPaymentId());
    }
    
    // 更新订单状态
    order.setStatus(OrderStatus.CANCELLED);
    orderRepository.save(order);
}</code></pre>
            </div>
        </div>

        <!-- Best Practices -->
        <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-2xl p-8 mb-16">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 text-center">
                <i class="fas fa-lightbulb mr-3"></i>最佳实践建议
            </h2>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl p-6">
                    <h4 class="font-bold text-lg mb-3 text-purple-700">
                        <i class="fas fa-tasks mr-2"></i>定期数据对账
                    </h4>
                    <p class="text-gray-600">
                        定期进行数据一致性检查，确保订单和支付数据的一致性。对于历史数据进行对账，发现不一致时进行人工干预和修复。
                    </p>
                </div>
                <div class="bg-white rounded-xl p-6">
                    <h4 class="font-bold text-lg mb-3 text-purple-700">
                        <i class="fas fa-clock mr-2"></i>设置合理超时
                    </h4>
                    <p class="text-gray-600">
                        为各个操作设置合理的超时时间，避免长时间占用资源。超时后自动触发补偿机制或告警通知。
                    </p>
                </div>
                <div class="bg-white rounded-xl p-6">
                    <h4 class="font-bold text-lg mb-3 text-purple-700">
                        <i class="fas fa-history mr-2"></i>操作日志记录
                    </h4>